import 'package:flutter/material.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/refresh_listview.dart';
import 'package:gzmall_app/model/coupon_item.dart';
import 'package:gzmall_app/store/store_detail_page.dart';

class MineCouponListItemWidget extends StatefulWidget {
  String state;

  MineCouponListItemWidget(this.state);
  @override
  State<StatefulWidget> createState() => MineCouponListItemWidgetState(state);
}

class MineCouponListItemWidgetState extends State<MineCouponListItemWidget> {
  String state;
  List<CouponItem> _coupons = [];

  int _curPage = 1;
  RefreshListView _refreshListView;
  bool _showEmpty = false;

  MineCouponListItemWidgetState(this.state);

  @override
  void initState() {
    super.initState();
    _getUserCoupons(state, 1);
  }

  @override
  Widget build(BuildContext context) {
    _refreshListView = RefreshListView(
      enablePullUp: true,
      enablePullDown: true,
      onRefresh: _onRefresh,
      child: _createCouponListView(),
      showEmpty: _showEmpty,
      onReload: () {
        _getUserCoupons(state, 1);
      },
    );
    return _refreshListView;
  }

  _createCouponListView() {
    return ListView.builder(
      itemCount: _coupons.length,
      itemBuilder: (context, index) {
        CouponItem _couponItem = _coupons[index];
        return InkWell(
          onTap: () {
            if (_couponItem != null &&
                _couponItem.coupon != null &&
                _couponItem.coupon.shop != null) {
              Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
                return new StoreDetailPage(store: _couponItem.coupon.shop);
              }));
            }
          },
          child: Container(
            alignment: Alignment.center,
            margin: const EdgeInsets.all(10.0),
            padding: const EdgeInsets.only(
                left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
            decoration: new BoxDecoration(
              image: new DecorationImage(
                  fit: BoxFit.fill,
                  image: new AssetImage("images/coupon_bg.png")),
            ),
            child: new Column(
              children: <Widget>[
                new Row(
                  children: <Widget>[
                    new Expanded(
                      child: new Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          new Text(
                            "¥" +
                                (_couponItem?.coupon?.money != null
                                    ? _couponItem.coupon.money.toString()
                                    : "0.0"),
                            style: new TextStyle(
                                fontSize: 30.0, color: ColorCommon.black1),
                          ),
                          new Text(
                            (_couponItem?.coupon?.fullMoney != null &&
                                    double.parse(_couponItem.coupon.fullMoney) >
                                        0
                                ? "满¥" +
                                    _couponItem.coupon.fullMoney.toString() +
                                    "可使用"
                                : "无金额门槛"),
                            style: new TextStyle(
                                fontSize: 13.0, color: ColorCommon.black1),
                          ),
                        ],
                      ),
                    ),
                    new Expanded(
                      flex: 2,
                      child: new Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          new Row(
                            children: <Widget>[
                              new Container(
                                padding: const EdgeInsets.all(2.0),
                                decoration: new BoxDecoration(
                                  color: ColorCommon.primaryColor,
                                  borderRadius: BorderRadius.circular(100.0),
                                ),
                                child: new Text(
                                  "现金券",
                                  style: new TextStyle(
                                      fontSize: 12.0, color: Colors.white),
                                ),
                              ),
                              new Padding(
                                padding: const EdgeInsets.only(left: 8.0),
                                child: new Text(
                                  (_couponItem?.coupon?.name != null
                                      ? _couponItem.coupon.name.toString()
                                      : ""),
                                  style: new TextStyle(
                                      fontSize: 15.0, color: Colors.white),
                                ),
                              ),
                            ],
                          ),
                          new Padding(
                            padding:
                                const EdgeInsets.only(top: 5.0, bottom: 5.0),
                            child: new Text(
                              (_couponItem?.coupon?.startAt != null
                                      ? _couponItem.coupon.startAt.toString()
                                      : "") +
                                  " 至 \n" +
                                  (_couponItem?.coupon?.endAt != null
                                      ? _couponItem.coupon.endAt.toString()
                                      : ""),
                              style: new TextStyle(
                                  fontSize: 13.0, color: Colors.white),
                            ),
                          ),
                          new Container(
                            padding: const EdgeInsets.only(
                                left: 10.0, right: 10.0, top: 2.0, bottom: 2.0),
                            decoration: new BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(100.0),
                            ),
                            child: new Text(
                              "立即使用",
                              style: new TextStyle(
                                  fontSize: 13.0, color: ColorCommon.black1),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                new Row(
                  children: <Widget>[
                    new Expanded(
                      child: new Container(
                        margin: const EdgeInsets.only(top: 8.0, bottom: 4.0),
                        child: new Text(
                          (_couponItem?.coupon?.remark != null
                              ? _couponItem.coupon.remark.toString()
                              : ""),
                          style: new TextStyle(
                              fontSize: 13.0, color: Colors.white),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  void _onRefresh(bool up) {
    if (up) {
      _getUserCoupons(state, 1);
    } else {
      _getUserCoupons(state, _curPage + 1);
    }
  }

  _getUserCoupons(String state, int page) {
    bool isGetNew = page == 1;
    Map<String, String> params = {"state": state, "page": page.toString()};
    NetUtil.get(
        context,
        Api.userCoupons,
        (data, message) {
          if (!mounted) return;
          _curPage = page;
          var _dataList = (data as List)
              ?.map((e) => e == null ? null : CouponItem.fromJson(e))
              ?.toList();
          setState(() {
            _showEmpty =
                isGetNew && (_dataList == null || _dataList.length <= 0);
            if (isGetNew) {
              _coupons = _dataList;
            } else {
              _coupons.addAll(_dataList);
            }
          });
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew,
                hasMore: _dataList != null && _dataList.length >= Api.pageSize);
          }
        },
        params: params,
        errorCallBack: (e) {
          setState(() {
            _showEmpty = isGetNew && (_coupons == null || _coupons.length <= 0);
          });
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew);
          }
        });
  }
}
